package com.zhentao.controller;

import com.zhentao.dto.system.LoginDto;
import com.zhentao.entity.system.SysUser;
import com.zhentao.service.SysMenuService;
import com.zhentao.service.SysUserService;
import com.zhentao.service.ValidateCodeService;
import com.zhentao.utils.AuthContextUtil;
import com.zhentao.vo.common.Result;
import com.zhentao.vo.common.ResultCodeEnum;
import com.zhentao.vo.system.LoginVo;
import com.zhentao.vo.system.SysMenuVo;
import com.zhentao.vo.system.ValidateCodeVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "用户接口")
@RestController
@RequestMapping("/admin/system/index")
public class IndexController {
    @Resource
    private SysUserService sysUserService;
    @Resource
    private ValidateCodeService validateCodeService;
    @Resource
    private SysMenuService sysMenuService;
    /**
     * 登录
     * @param loginDto
     * @return
     */
    @PostMapping("/login")
    @Operation(summary = "登录接口")
    public Result<LoginVo> login(@RequestBody LoginDto loginDto){
        LoginVo loginVo = sysUserService.login(loginDto);
        return Result.build(loginVo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 获取验证码
     * @return
     */

    @GetMapping(value = "/generateValidateCode")
    public Result<ValidateCodeVo> generateValidateCode() {
        ValidateCodeVo validateCodeVo = validateCodeService.generateValidateCode();
        return Result.build(validateCodeVo , ResultCodeEnum.SUCCESS) ;
    }

    /**
     * 获取用户信息
     * @return
     */
    @GetMapping(value = "/getUserInfo")
    public Result<SysUser> getUserInfo() {
        return Result.build(AuthContextUtil.get(),ResultCodeEnum.SUCCESS);
    }

    /**
     * 退出登录
     * @param token
     * @return
     */
    @GetMapping("/logout")
    public Result logout(@RequestHeader("token")String token){
        sysUserService.logout(token);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    /**
     * 动态菜单
     * @return
     */
    @GetMapping("/menus")
    public Result menus() {
        List<SysMenuVo> sysMenuVoList = sysMenuService.findUserMenuList() ;
        return Result.build(sysMenuVoList , ResultCodeEnum.SUCCESS) ;
    }
}